package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import org.json.JSONException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理_OA质量信息接口(外围系统->SAP)	回传接口 新增
 */
public class YNMMI032Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }

        String post = null;
        try {
            JSONObject result = new JSONObject();
            result.put("QMNUM", mainTableInfoMap.get("tzdh"));
            result.put("PRUEFLOS", mainTableInfoMap.get("jyp"));
            result.put("VCODE", mainTableInfoMap.get("jyclfasap"));
            baseBean.writeLog("wyq==MMI032数据回传====object" + result.toString());
            post = YNHttpUtils.MMI032(result.toString(), YNHttpUtils.token());
            baseBean.writeLog("wyq===MMI032数据回传===post" + post);
            JSONObject object =  JSONObject.parseObject(post);
            String status = object.getString("TYPE");
            String message = object.getString("MESSAGE");
            if (!status.equals("S")) {
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid() + "MMI032接口返回信息");
                requestInfo.getRequestManager().setMessagecontent("MMI032接口返回信息" + message);
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (JSONException e) {
            writeLog("wyq==MMI032数据回传====error:" + e.getMessage());
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid() + "MMI032接口返回信息");
            requestInfo.getRequestManager().setMessagecontent("MMI032接口返回信息error" + e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
